﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace TimeClock
{
    public partial class ConfigWindow : Form
    {
        private System.Timers.Timer timer = new System.Timers.Timer(1000);
        private int timer_shengyu = 0;
        private int timer_set = 0;
        private FloatWindow floatWindow = new FloatWindow();
        private int timer_type = 0;
        public ConfigWindow()
        {
            InitializeComponent();
            timer.Elapsed += Timer_Elapsed;
            timer.AutoReset = true;
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {

            this.Invoke((Action)(() =>
            {
                Console.WriteLine(timer_shengyu);
                try
                {
                    TimeSpan timeSpan = TimeSpan.FromSeconds(timer_shengyu);
                    string formattedTime = timeSpan.ToString(@"mm\:ss");
                    floatWindow.update_timer_text(formattedTime);
                }
                catch (Exception ex) { 
                    Console.WriteLine(ex.ToString());
                }
               
                timer_shengyu -= 1;
                if (timer_shengyu <= 0)
                {
                    floatWindow.update_timer_text("//://");
                    floatWindow.Hide();
                    timer.Stop();
                    FullWindow fullWindow = new FullWindow(this);
                    fullWindow.Show();
                }
            }));

           

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                timer_type = comboBox1.SelectedIndex;
                timer_shengyu = Convert.ToInt32(textBox1.Text) * 60;
                timer_set = timer_shengyu;
                timer.Stop();
                timer.Start();
                MessageBox.Show("倒计时已开始，双击托盘图标打开界面，右击图标退出程序");
                notifyIcon1.Visible = true;
                WindowState = FormWindowState.Minimized;
                floatWindow.Show();
                Hide();
            }
            catch (Exception ex) { 
                MessageBox.Show("输入的数字不正确，必须是整数");
            }
        }

        private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)
        {
            // 创建一个ContextMenu对象
            ContextMenu contextMenu = new ContextMenu();

            // 创建一个MenuItem对象，表示关闭软件的选项
            MenuItem menuItem = new MenuItem("关闭软件");

            // 将MenuItem对象添加到ContextMenu对象中
            contextMenu.MenuItems.Add(menuItem);

            // 将ContextMenu对象赋值给notifyIcon的ContextMenuStrip属性
            notifyIcon1.ContextMenu = contextMenu;

            // 添加关闭软件的事件处理程序
            menuItem.Click += (sender1, e1) =>
            {
                // 在这里编写关闭软件的逻辑
                // 例如：关闭主窗体
                Application.Exit();
            };
        }

        private void 显示界面_Click(object sender, EventArgs e)
        {

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            notifyIcon1.Visible = false;
            Show();
            WindowState = FormWindowState.Normal;
        }

        public void timer_start()
        {
            if(timer_type == 1)
            {
                notifyIcon1.Visible = false;
                Show();
                WindowState = FormWindowState.Normal;
            }
            else
            {
                timer_shengyu = timer_set;
                timer.Stop();
                timer.Start();
                floatWindow.Show();
            }
            
        }

    }
}
